<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Gmap
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Gmap_Form_Admin_Manage_Filter extends Engine_Form
{
  
  public function init()
  {
    $this->clearDecorators()
      ->addDecorator('FormElements')
      ->addDecorator('Form')
      ->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'search'))
      ->addDecorator('HtmlTag2', array('tag' => 'div', 'class' => 'clear'));

    $this
      ->setAttribs(array(
        'id' => 'gmap_admin_filter_form',
        'class' => 'global_form_box',
      ));

 
    $categories = Engine_Api::_()->gmap()->getCategories();
    $categories_prepared = Engine_Api::_()->gmap()->convertCategoriesToArray($categories);
    $categories_prepared = array(""=>"") + $categories_prepared;
    
    $this->addElement('Select', 'category', array(
      'label' => 'Category',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),
      'multiOptions' => $categories_prepared,      
    ));        
      
    $this->addElement('Text', 'search', array(
      'label' => 'Keyword',
      'attribs' => array('size' => 14),
    ));  
    
    $this->addElement('Text', 'username', array(
      'label' => 'Username',
      'attribs' => array('size' => 14),
    ));  


    $this->addElement('Text', 'location', array(
      'label' => 'Location',
      'attribs' => array('size' => 14),
    )); 
    
    $unit = Engine_Api::_()->getApi('settings', 'core')->getSetting('gmap.distanceunit', Radcodes_Lib_Helper_Unit::UNIT_MILE);
    
    $distances = array('0'=>"");
    $distance_ranges = array(1,5,10,25,50,100,250,500);
    foreach ($distance_ranges as $distance) {
      $distances[$distance] = "$distance $unit";
    }
    
    $this->addElement('Select', 'distance', array(
      'label' => 'Within Distance',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),    
      'multiOptions' => $distances,
    ));

    $yes_no = array(
        '' => '',
        '1' => 'Yes',
        '0' => 'No',
    );  
    
    $this->addElement('Select', 'featured', array(
      'label' => 'Featured',
      'multiOptions' => $yes_no,
    ));
    
    $this->addElement('Select', 'sponsored', array(
      'label' => 'Sponsored',
      'multiOptions' => $yes_no,
    ));

    $this->addElement('Select', 'home', array(
      'label' => 'Home',
      'multiOptions' => $yes_no,
    ));    


    foreach( $this->getElements() as $fel ) {
      if( $fel instanceof Zend_Form_Element ) {
        
        $fel->clearDecorators()
          ->addDecorator('ViewHelper')
          ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
          ->addDecorator('HtmlTag', array('tag' => 'div', 'id'  => $fel->getName() . '-search-wrapper', 'class' => 'form-search-wrapper'));
        
      }
    }  
    
   // $submit = new Engine_Form_Element_Submit('filtersubmit', array('type' => 'submit'));
    $submit = new Engine_Form_Element_Button('filtersubmit', array('type' => 'submit'));
    $submit
      ->setLabel('Search')
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'buttons'))
      ->addDecorator('HtmlTag2', array('tag' => 'div'));

    $this->addElement($submit);
      
    $this->addElement('Hidden', 'order', array(
      'order' => 1001,
    ));

    $this->addElement('Hidden', 'order_direction', array(
      'order' => 1002,
    ));


    // Set default action
    $this->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module'=>'gmap', 'controller'=>'manage'), 'admin_default', true));
  }   
  
  public function initBAK()
  {
    $this->clearDecorators()
      ->addDecorator('FormElements')
      ->addDecorator('Form')
      ->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'search'))
      ->addDecorator('HtmlTag2', array('tag' => 'div', 'class' => 'clear'));

    $this
      ->setAttribs(array(
        'id' => 'filter_form',
        'class' => 'global_form_box',
      ));

    $search = new Zend_Form_Element_Text('search');
    $search
      ->setLabel('Keyword')
      ->setAttrib('size', 10)
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div'));

    $location = new Zend_Form_Element_Text('location');
    $location
      ->setLabel('Location')
      ->setAttrib('size', 10)
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div'));      
      
    $unit = Engine_Api::_()->getApi('settings', 'core')->getSetting('gmap.distanceunit', Radcodes_Lib_Helper_Unit::UNIT_MILE);
    
    $distances = array('0'=>"");
    $distance_ranges = array(1,5,10,25,50,100,250,500,1000);
    foreach ($distance_ranges as $distance) {
      $distances[$distance] = "$distance $unit";
    }
    
    $distance = new Zend_Form_Element_Select('distance');
    $distance
      ->setLabel('Distance')
      ->setFilters(array(
        new Radcodes_Lib_Filter_Null()
      ))
      ->setMultiOptions($distances)
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div')); 

      
    $username = new Zend_Form_Element_Text('username');
    $username
      ->setLabel('Username')
      ->setAttrib('size', 10)
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div')); 

    $featured = new Zend_Form_Element_Select('is_featured');
    $featured
      ->setLabel('Featured')
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div'))
      ->setMultiOptions(array(
        '' => '',
        '1' => 'Yes',
        '0' => 'No',
      ));

    $sponsored = new Zend_Form_Element_Select('is_sponsored');
    $sponsored
      ->setLabel('Sponsored')
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div'))
      ->setMultiOptions(array(
        '' => '',
        '1' => 'Yes',
        '0' => 'No',
      ));
      

    $home = new Zend_Form_Element_Select('is_home');
    $home
      ->setLabel('Home')
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('Label', array('tag' => null, 'placement' => 'PREPEND'))
      ->addDecorator('HtmlTag', array('tag' => 'div'))
      ->setMultiOptions(array(
        '' => '',
        '1' => 'Yes',
        '0' => 'No',
      ));
      
    $submit = new Zend_Form_Element_Button('filtersubmit', array('type' => 'submit'));
    $submit
      ->setLabel('Search')
      ->clearDecorators()
      ->addDecorator('ViewHelper')
      ->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'buttons'))
      ->addDecorator('HtmlTag2', array('tag' => 'div'));

    $this->addElement('Hidden', 'order', array(
      'order' => 1001,
    ));

    $this->addElement('Hidden', 'order_direction', array(
      'order' => 1002,
    ));

    
    $this->addElements(array(
      $search,
      $username,
      $location,
      $distance,
      $featured,
      $sponsored,
      $home,
      $submit,
    ));

    // Set default action
    $this->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module'=>'gmap', 'controller'=>'manage'), 'admin_default', true));
  }
}